package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SimMallOrderBo;
import org.dromara.system.domain.vo.SimMallOrderVo;
import org.dromara.system.service.ISimMallOrderService;
import org.dromara.system.service.impl.permission.PermissionBaseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商城订单
 *
 * @author Lion Li
 * @date 2025-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mallOrder")
public class SimMallOrderController extends BaseController {

    private final ISimMallOrderService simMallOrderService;

    /**
     * 查询商城订单列表
     */
//    @SaCheckPermission("system:mallOrder:list")
    @GetMapping("/list")
    public TableDataInfo<SimMallOrderVo> list(SimMallOrderBo bo, PageQuery pageQuery) {
        return simMallOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商城订单列表
     */
//    @SaCheckPermission("system:mallOrder:export")
    @Log(title = "商城订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SimMallOrderBo bo, HttpServletResponse response) {
        List<SimMallOrderVo> list = simMallOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "商城订单", SimMallOrderVo.class, response);
    }

    /**
     * 获取商城订单详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:mallOrder:query")
    @GetMapping("/{id}")
    public R<SimMallOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(simMallOrderService.queryById(id));
    }

    /**
     * 新增商城订单
     */
//    @SaCheckPermission("system:mallOrder:add")
    @Log(title = "商城订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SimMallOrderBo bo) {
        return toAjax(simMallOrderService.insertByBo(bo));
    }

    /**
     * 修改商城订单
     */
//    @SaCheckPermission("system:mallOrder:edit")
    @Log(title = "商城订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SimMallOrderBo bo) {
        return toAjax(simMallOrderService.updateByBo(bo));
    }

    /**
     * 删除商城订单
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:mallOrder:remove")
    @Log(title = "商城订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(simMallOrderService.deleteWithValidByIds(List.of(ids), true));
    }


    @Log(title = "商城订单下单", businessType = BusinessType.INSERT)
    @PostMapping("/createMallOrder")
    public R<String> createMallOrder(@RequestBody SimMallOrderBo bo) {
        return R.ok("创建订单成功！", simMallOrderService.createMallOrder(bo));
    }


    //微信支付回调
    @PostMapping(value = "/callBak/wxPay")
    @SaIgnore
    public String wxPayCallback(HttpServletRequest request, HttpServletResponse response) {
        return simMallOrderService.wxPayCallback(request, response);
    }


}
